#include <iostream>
#include <unistd.h>
#include <string.h>
#include <cstdio>

class Task
{
public:
    Task(int a, int b)
        :_a(a)
        ,_b(b)
    {
    }

    int Excute()
    {
        return _a + _b;
    }

    ~Task()
    {
    }
private:
    int _a;
    int _b;
};

class Result
{
public:
    Result(int result) : _result(result)
    {
    }

    int GetResult()
    {
        return _result;
    }

    ~Result()
    {}

private:
    int _result;
};

void *routine(void *args)
{
    Task *t = static_cast<Task*>(args);
    Result *res = new Result(t->Excute());
    sleep(1);
    pthread_exit(res);
    std::cout << "haha, 新线程不应该看到这里!" << std::endl;
}


int main()
{
    pthread_t tid;
    Task *t = new Task(10, 20);
    int n = pthread_create(&tid, nullptr, routine, t);

    int cnt = 5;
    while(cnt--)
    {
        std::cout << "main线程名字：" << std::endl;
        sleep(1);
    }

    Result* ret = nullptr;
    n = pthread_join(tid, (void**)&ret);
    if(n != 0)
    {
        std::cout << "pthread_join error: " << strerror(n) << std::endl;
    }
    else{
        std::cout << "pthread_join succsee: " << strerror(n) << std::endl;
    }
    n = ret->GetResult();
    std::cout << "新线程结束，运行结果是： " << n << std::endl;
    delete ret;
    delete t;
    return 0;
}